//========================================================================
// This file was generated using the MyGeneration tool in combination
// with the Gentle.NET Business Entity template, $Rev: 1104 $
//========================================================================
using System;
using System.Collections;
using Gentle.Common;
using Gentle.Framework;

namespace GrewalCRM.Entities
{
	/// <summary>
	/// Instances of this class represent the properties and methods of a row in the table <b>Reff_Status</b>.
	/// </summary>
	[TableName("Reff_Status", CacheStrategy.Temporary)]
	public class ReffStatus : BaseEntitiesGrewalCRM
	{
		#region Members
		private bool isChanged;
		private int statusID;
		private string statusName;
		private string description;
		#endregion
			
		#region Constructors
		/// <summary>
		/// Create a new instance using the default constructor
		/// </summary>
		public ReffStatus()
		{
			isChanged = true;
		}

		/// <summary> 
		/// Create a new object using the minimum required information (all not-null fields except 
		/// auto-generated primary keys). 
		/// </summary> 
		public ReffStatus()
		{
			isChanged = true;
			statusID = 0;
		}

		/// <summary> 
		/// Create a new object by specifying all fields (except the auto-generated primary key field). 
		/// </summary> 
		public ReffStatus(string statusName, string description)
		{
			isChanged = true;
			this.statusName = statusName;
			this.description = description;
		}
			
		/// <summary> 
		/// Create an object from an existing row of data. This will be used by Gentle to 
		/// construct objects from retrieved rows. 
		/// </summary> 
		public ReffStatus(int statusID, string statusName, string description)
		{
			this.statusID = statusID;
			this.statusName = statusName;
			this.description = description;
		}
		#endregion

		#region Public Properties
		/// <summary>
		/// Indicates whether the entity is changed and requires saving or not.
		/// </summary>
		public bool IsChanged
		{
			get { return isChanged; }
		}

		/// <summary>
		/// Property relating to database column StatusID
		/// </summary>
		[TableColumn("StatusID", NotNull=true), PrimaryKey(AutoGenerated=true)]
		public int StatusID
		{
			get { return statusID; }
			set { isChanged |= statusID != value; statusID = value; }
		}

		/// <summary>
		/// Property relating to database column StatusName
		/// </summary>
		[TableColumn("StatusName", NullValue="")]
		public string StatusName
		{
			get { return statusName != null ? statusName.TrimEnd() : string.Empty; }
			set { isChanged |= statusName != value; statusName = value; }
		}

		/// <summary>
		/// Property relating to database column Description
		/// </summary>
		[TableColumn("Description", NullValue="")]
		public string Description
		{
			get { return description != null ? description.TrimEnd() : string.Empty; }
			set { isChanged |= description != value; description = value; }
		}
		#endregion

		#region Storage and Retrieval
	
		/// <summary>
		/// Static method to retrieve all instances that are stored in the database in one call
		/// </summary>
		public static IList ListAll()
		{
			return Broker.RetrieveList(typeof(ReffStatus));
		}

		/// <summary>
		/// Retrieves an entity given it's id.
		/// </summary>
		public static ReffStatus Retrieve(int id)
		{
			Key key = new Key(typeof(ReffStatus), true, "StatusID", id);
			return Broker.RetrieveInstance(typeof(ReffStatus), key) as ReffStatus;
		}
		
		/// <summary>
		/// Retrieves an entity given it's id, using Gentle.Framework.Key class.
		/// This allows retrieval based on multi-column keys.
		/// </summary>
		public static ReffStatus Retrieve(Key key)
		{
			return Broker.RetrieveInstance(typeof(ReffStatus), key) as ReffStatus;
		}
		
		/// <summary>
		/// Persists the entity if it was never persisted or was changed.
		/// </summary>
		public override void Persist()
		{
			if (IsChanged || !IsPersisted)
			{
				base.Persist();
				isChanged = false;
			}
		}

		#endregion


		#region Relations

		/// <summary>
		/// Get a list of Calls referring to the current entity.
		/// </summary>
				
		public IList ReferringCalls()
		{
			//select * from 'foreigntable'
			SqlBuilder sb = new SqlBuilder(StatementType.Select, typeof(Calls));

			// where foreigntable.foreignkey = ourprimarykey
			sb.AddConstraint(Operator.Equals, "CallStatusID", StatusID);

			// passing true indicates that we'd like a list of elements, i.e. that no primary key
			// constraints from the type being retrieved should be added to the statement
			SqlStatement stmt = sb.GetStatement(true);

			// execute the statement/query and create a collection of User instances from the result set
			return ObjectFactory.GetCollection(typeof(Calls), stmt.Execute());

			// TODO In the end, a GentleList should be returned instead of an arraylist
			//return new GentleList( typeof(Calls), this );
		}
		#endregion
	}
}
